import http from '../common/http.js'
// 定义一个混入分页对象
var page = {
	data: function() {
		return {
			pageSizeName: "pageSize",
			pageNoName: "pageNo",
			pageTotalName: "total",
			pageSize: 12,
			pageNo: 0, //页码
			pageCount: 0, //总页数
			listData_: {
				list: [],
				loadStatus: "more"
			},
			currentTab: null,
			tabs: { //标签页数据
				/* 1:listData_: {
					pageNo: 0, //页码
					pageCount: 0, //总页数
					list: [],
					loadStatus: "more"
				} */
			}
		}
	},
	computed: {
		listData: function() {
			return this.listData_;
		}
	},
	watch: {
		tabs: { //深度监听，可监听到对象、数组的变化  弃用有时候不灵
			handler(val, oldVal) {
				//this.listData_ = this.tabs[this.currentTab].listData_.list
				//console.error(JSON.stringify(this.tabs[tab].listData_.list))
				//console.error(JSON.stringify(this.listData_ ))
				//console.error(this.currentTab)
			},
			deep: true
		}
	},
	methods: {
		fetchList(url, data, callback) { //分页获取列表数据
			var that = this;
			if (this.pageNo != 0 && this.pageNo >= this.pageCount) { //
				this.listData_.loadStatus = "noMore"
			} else {
				typeof(data) == "function" && (callback = data, data = {})
				data = data || {};
				this.listData_.loadStatus = "loading";
				this.pageNo += 1;
				http.get(url, Object.assign(data, {
					pageNo: that.pageNo
				})).then(res => {
					this.listData_.list.push.apply(this.listData_.list, res.data.items);
					this.pageCount = res.data.pageCount;
					that.pageNo >= that.pageCount ? that.listData_.loadStatus = "noMore" : this.listData_.loadStatus = "more";
					//console.log(this.pageCount)
					//console.error(res)
				}).catch(e => console.log(e))
			}
		},
		initTabs(tabs) { //如果是 tab页面 先在onload 事件中 初始化
			var that = this
			var g = {}
			for (var idx in tabs) {
				g[idx] = {
					listData_: {
						pageNo: 0, //页码
						pageCount: 0, //总页数
						list: [],
						loadStatus: "more"
					}
				}
				//console.error(idx)
			}
			this.tabs = Object.assign({}, this.tabs, g);
		},
		//tab 标签标示
		fetchTabList(tab, url, data, callback) { //分页获取列表数据
			var that = this;
			this.currentTab = tab		
			if (this.tabs[tab] && this.tabs[tab].listData_.pageNo != 0 && this.tabs[tab].listData_.pageNo >= this.tabs[tab].listData_
				.pageCount) { //
				//this.tabs[tab].listData_.loadStatus = "noMore"
				this.tabs[tab].listData_ = Object.assign({}, this.tabs[tab].listData_, {
					loadStatus: "noMore"
				});
				this.listData_ = this.tabs[this.currentTab].listData_
				//console.log(`数据${this.currentTab}`+JSON.stringify(this.listData))
			} else {
				typeof(data) == "function" && (callback = data, data = {})
				data = data || {};
				this.tabs[tab].listData_.loadStatus = "loading";
				this.tabs[tab].listData_.pageNo += 1;
				http.get(url, Object.assign(data, {
					pageNo: that.tabs[tab].listData_.pageNo
				})).then(res => {					
					this.tabs[tab].listData_.list.push.apply(this.tabs[tab].listData_.list, res.data.items);
					this.tabs[tab].listData_.pageCount = res.data.pageCount;
					that.tabs[tab].listData_.pageNo >= that.tabs[tab].listData_.pageCount ? that.tabs[tab].listData_.loadStatus =
						"noMore" : this.tabs[tab].listData_.loadStatus = "more";
						
					this.listData_ = this.tabs[this.currentTab].listData_ 
					//console.log(`数据${this.currentTab}`+JSON.stringify(this.listData))
				}).catch(e => console.log(e))
			}
		},
		async fetchAll(url, data, callback) { //不分页获取列表数据
			var that = this,
				ret = [];
			typeof(data) == "function" && (callback = data, data = {})
			data = data || {};
			await http.get(url, Object.assign(data, {
				pageNo: -1
			})).then(res => {
				ret = callback ? callback.call(this, res.data.items) : res.data;
				//console.log(this.pageCount)
			}).catch(e => console.log(e))
			return ret;
		}
	}
}
export default page
